# Author: Allen Hutchison
# Copyright (C) 2006 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#      Unless required by applicable law or agreed to in writing, software
#      distributed under the License is distributed on an "AS IS" BASIS,
#      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#      See the License for the specific language governing permissions and
#      limitations under the License.
#
# This Oaf Package is an example of a results reporting agent.
# This particular agent saves results in text format in the 
# $base_path/logs directory.

package Oaf::Results::Text;
use base qw(Oaf::Results);

use strict;
use warnings;

# new()
#  Create a new instance of Oaf::Result::Text.
sub new {
  my ($class, @args) = @_;

  # Start with an Oaf::Result object.
  # We'll end up overriding some of the
  # methods in that package.
  my $self = $class->SUPER::new(@args);

  bless $self, $class;

  $self->find_log_dir();

  return $self;
}

# log_results()
#  Log results from an individual test. In this case we'll
#  write them to a text file in the log_dir.
sub log_results {
  # $test is an Oaf::Test object
  my($self, $test) = @_;

  my $log_dir = $self->get_param('oaf', 'log_dir');

  # Grab the actual command name.
  # TODO: Make OS Neutral in next release
  my $prog = $test->test();
  $prog =~ s{.*/(\w+)$}{$1};
  my $file = "$log_dir/" . $test->start_time() . "-$prog";

  open(FILE, ">$file") or $self->fatal_error("Can't open file $file: $!");

  print FILE "-" x 80, "\n";
  print FILE "Test ID: ", $test->id(), "\n";
  print FILE "Program: ", $test->test(), "\n";
  print FILE "Arguments: ", $test->args(), "\n";
  print FILE "PID: ", $test->pid(), "\n";
  print FILE "Start Time: ", $test->start_time_stamp(), "\n";
  print FILE "End Time: ", $test->end_time_stamp(), "\n";
  print FILE "Duration: ", seconds_to_string($test->duration()), "\n";
  print FILE "Exit Code: ", $test->exit_code(), " ", 
             $test->exit_code_string(), "\n";
  print FILE "-" x 80, "\n";

  foreach my $line (@{$test->output()}) {
    print FILE "$line\n";
  }
  close FILE;
}


# notify()
#  This routine is used to take the summary data and do
#  what you want with it. In this case notify will write
#  the data out to a file in the log_dir, but in other
#  cases you might email it to a group or post it to a www page.
sub notify {
  my($self) = @_;

  my $log_dir = $self->get_param('oaf', 'log_dir');
  my $file = "$log_dir/summary.txt";

  open(FILE, ">$file") or $self->fatal_error("Can't open file $file $!");

  print FILE "Tests Passed: ", $self->test_passed_count(), " ",
             $self->test_passed_percent(), "%\n";
  print FILE "Tests Failed: ", $self->test_failed_count(), " ",
             $self->test_failed_percent(), "%\n";
  print FILE "Total Test Time: ", seconds_to_string($self->duration()), "\n";
  print FILE "-" x 80, "\n";
  print FILE "Failed Tests:\n";
  print FILE "-" x 80, "\n";
  foreach my $test (@{$self->tests_failed()}) {
    print FILE $test->id(), " ", $test->test(), "\n";
  }
  close FILE;
}

# find_log_dir()
#  Figure out where the logs directory is. It should be right at
#  the top of base_path.
sub find_log_dir {
  my($self) = @_;
  my $base_path = $self->get_param('oaf', 'base_path');
  my $harness_start = $self->get_param('oaf', 'harness_start_time', time);

  if ((defined($base_path)) and
      (defined($harness_start))) {
    mkdir "$base_path/logs/$harness_start/" or
      $self->fatal_error("Couldn't Create $harness_start directory " .
			 "in $base_path/logs/");
    $self->set_param('oaf', 'log_dir', "$base_path/logs/$harness_start");
  } else {
    $self->fatal_error("Can't find oaf base path or harness start time");
  }
}

# seconds_to_string()
#  Convert an elapsed time in seconds to a more human readable
#  time. Uses gmtime to do something it's probably not supposed to do, but
#  this is a fairly well accpeted perl trick:
#  http://www.perlmonks.org/?node_id=30392
sub seconds_to_string {
  my ($self, $seconds) = @_;

  $seconds++; # Convert seconds to start from 1

  if ($seconds > 0) {
    return join ":", map { sprintf "%02d", $_ } (gmtime($seconds))[7, 2, 1, 0];
  } else {
    return 0;
  }
}

# Have to eval to something
1;
__END__
